{"componentChunkName":"component---src-templates-blog-post-js","path":"/react/router/","result":{"data":{"site":{"siteMetadata":{"title":"Tory","author":"[Tory]","siteUrl":"https://gatsby-starter-bee.netlify.com","comment":{"disqusShortName":"","utterances":"JaeYeopHan/gatsby-starter-bee"},"sponsor":{"buyMeACoffeeId":"jbee"}}},"markdownRemark":{"id":"caff49a8-71db-5ebe-b7b3-088316f42255","excerpt":"1. Before Jumpo into React Router ROUTER에 대해 학습하기 전 SSR과 CSR에 대해 간략하게 알고 넘어가자!\nSSR과 CSR은 페이지와 관련이 깊다. 페이지의 구조를 담당하는 HTML 파일을 어느 위치에서 생성할 것인지에 따라 SSR과 CSR로 나뉘게 된다. 1.1 SSR SSR은 사용자가 서버에게 페이지 리소스를 요청하면, 서버는 요청에 해당하는 HTML 파일을 생성하여 전송한다. 장점 HTML…","html":"<h2 id=\"1-before-jumpo-into-react-router\" style=\"position:relative;\"><a href=\"#1-before-jumpo-into-react-router\" aria-label=\"1 before jumpo into react router permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Before Jumpo into React Router</h2>\n<p>ROUTER에 대해 학습하기 전 SSR과 CSR에 대해 간략하게 알고 넘어가자!\nSSR과 CSR은 페이지와 관련이 깊다. 페이지의 구조를 담당하는 HTML 파일을 어느 위치에서 생성할 것인지에 따라 SSR과 CSR로 나뉘게 된다.</p>\n<h3 id=\"11-ssr\" style=\"position:relative;\"><a href=\"#11-ssr\" aria-label=\"11 ssr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1.1 SSR</h3>\n<p>SSR은 사용자가 서버에게 페이지 리소스를 요청하면, 서버는 요청에 해당하는 HTML 파일을 생성하여 전송한다.</p>\n<h4 id=\"장점\" style=\"position:relative;\"><a href=\"#%EC%9E%A5%EC%A0%90\" aria-label=\"장점 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>장점</h4>\n<ul>\n<li>HTML 코드를 완성해서 보내기 때문에, 검색 엔진 최적화가 가능함.</li>\n<li>\n<p>첫 페이지 렌더링은 일반적으로 CSR보다 빠르다.</p>\n<ul>\n<li>CSR은 HTML을 생성하는 번들러를 받아온 후 HTML을 생성하는 과정이 필요하기 때문</li>\n</ul>\n</li>\n</ul>\n<h4 id=\"단점\" style=\"position:relative;\"><a href=\"#%EB%8B%A8%EC%A0%90\" aria-label=\"단점 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>단점</h4>\n<ul>\n<li>페이지마다 html 파일을 요청하기 때문에 요청 횟수가 증가한다.</li>\n<li>따라서 첫 페이지 렌러딩 외에는 일반적으로 CSR보다 렌더링 속도가 느리다.</li>\n</ul>\n<h3 id=\"12-csr\" style=\"position:relative;\"><a href=\"#12-csr\" aria-label=\"12 csr permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1.2. CSR</h3>\n<p>CSR은 초기에 최소한의 코드를 포함한 HTML을 전송 받은 후, JS 파일을 통해 HTML의 메인 코드들을 동적으로 생성합니다.</p>\n<h4 id=\"장점-1\" style=\"position:relative;\"><a href=\"#%EC%9E%A5%EC%A0%90-1\" aria-label=\"장점 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>장점</h4>\n<ul>\n<li>서버에 요청 수가 줄어듦에 따라 서버의 부담을 덜어 낼 수 있다.</li>\n<li>두번째 렌더링 부터는 HTML요청 없이 JS를 통해 렌더링하기 때문에 SSR 보다 빠르다.</li>\n</ul>\n<h4 id=\"단점-1\" style=\"position:relative;\"><a href=\"#%EB%8B%A8%EC%A0%90-1\" aria-label=\"단점 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>단점</h4>\n<ul>\n<li>최소한의 코드를 포함한 HTML은 검색 엔진에 전달할 정보가 부족하므로 포털에 SEO 점수가 낮게 평가됩니다.</li>\n<li>번들된 JS 파일의 크기가 커질 경우 초기 렌더링이 느려지기 때문에 최적화가 필요합니다.</li>\n</ul>\n<h2 id=\"2-react-router\" style=\"position:relative;\"><a href=\"#2-react-router\" aria-label=\"2 react router permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. React Router</h2>\n<p>클라이언트 사이드에서 라우팅하기 위한 라이브러리로 <strong>react-router-dom</strong>을 사용할 수 있다.</p>\n<ol>\n<li>Router: Route(경로)들을 관리한다.</li>\n<li>Route: 특정 경로(path)가 입력되면 component를 렌더링 한다.</li>\n<li>\n<p>Switch: 첫번째로 매칭되는 Route만 렌더링 되도록 하는 도구</p>\n<blockquote>\n<p>(switch가 없다면 루트 경로로 이동하고자 하는 경우 ”/“를 포함한 Route가 모두 렌더링 되는 문제가 발생한다. 아래 예시의 exact를 기입했음에도 dashboard, products, landing이 모두 렌더링 됨.)</p>\n</blockquote>\n</li>\n<li>Redirect</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span>\n  BrowserRouter <span class=\"token keyword\">as</span> Router<span class=\"token punctuation\">,</span>\n  Route<span class=\"token punctuation\">,</span>\n  Switch<span class=\"token punctuation\">,</span>\n  Redirect<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'react-router-dom'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Landing<span class=\"token punctuation\">,</span> Products<span class=\"token punctuation\">,</span> Dashboard <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'pages'</span><span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> Header<span class=\"token punctuation\">,</span> Footer <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'containers'</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token keyword\">function</span> <span class=\"token function\">WireframeApp</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>\n    <span class=\"token operator\">&lt;</span>Router<span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span>Switch<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Route exact path<span class=\"token operator\">=</span><span class=\"token string\">\"/\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>Landing<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Route path<span class=\"token operator\">=</span><span class=\"token string\">\"/dashboard\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>Dashboard<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Route path<span class=\"token operator\">=</span><span class=\"token string\">\"/products\"</span> component<span class=\"token operator\">=</span><span class=\"token punctuation\">{</span>Products<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Redirect to<span class=\"token operator\">=</span><span class=\"token string\">\"/landing\"</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>Switch<span class=\"token operator\">></span>\n    <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>Router<span class=\"token operator\">></span>\n  <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2 id=\"3-storybook-with-router\" style=\"position:relative;\"><a href=\"#3-storybook-with-router\" aria-label=\"3 storybook with router permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. storybook with router</h2>\n<p>리엑트의 경우 Link 컴포넌트를 통해 경로를 이동시킬 수 있다.</p>\n<p>이 때, Link는 Router 컴포넌트 내에서만 사용할 수 있기 때문에, Link 컴포넌트를 테스트하고 싶은 경우, decorator를 사용하여 Router를 감싸 줄 수 있다.</p>\n<div class=\"gatsby-highlight\" data-language=\"ts\"><pre class=\"language-ts\"><code class=\"language-ts\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token punctuation\">{</span>\n  title<span class=\"token operator\">:</span> <span class=\"token string\">'Components/Nav'</span><span class=\"token punctuation\">,</span>\n  component<span class=\"token operator\">:</span> Nav<span class=\"token punctuation\">,</span>\n  decorators<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n    Story <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span>\n      <span class=\"token operator\">&lt;</span>Router<span class=\"token operator\">></span>\n        <span class=\"token operator\">&lt;</span>Story <span class=\"token operator\">/</span><span class=\"token operator\">></span>\n      <span class=\"token operator\">&lt;</span><span class=\"token operator\">/</span>Router<span class=\"token operator\">></span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span> <span class=\"token keyword\">as</span> ComponentMeta<span class=\"token operator\">&lt;</span><span class=\"token keyword\">typeof</span> Nav<span class=\"token operator\">></span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">const</span> Template<span class=\"token operator\">:</span> ComponentStory<span class=\"token operator\">&lt;</span><span class=\"token keyword\">typeof</span> Nav<span class=\"token operator\">></span> <span class=\"token operator\">=</span> args <span class=\"token operator\">=></span> <span class=\"token operator\">&lt;</span>Nav <span class=\"token punctuation\">{</span><span class=\"token operator\">...</span>args<span class=\"token punctuation\">}</span> <span class=\"token operator\">/</span><span class=\"token operator\">></span><span class=\"token punctuation\">;</span>\n\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">const</span> Basic <span class=\"token operator\">=</span> <span class=\"token function\">Template</span><span class=\"token punctuation\">.</span><span class=\"token function\">bind</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>","frontmatter":{"title":"router","date":"September 02, 2021"}}},"pageContext":{"slug":"/react/router/","previous":{"fields":{"slug":"/typescript/typescript-redux/"},"frontmatter":{"title":"typescript-redux","category":"typescript","draft":false}},"next":{"fields":{"slug":"/css/vertical-aline/"},"frontmatter":{"title":"vertical-align","category":"css","draft":false}}}},"staticQueryHashes":["3128451518","521680639"]}